package com.example.dockerdemo.controller;

import com.example.dockerdemo.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @PostMapping("/login")
    public Map login(String username,String password){
        Map result = new HashMap();

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            result.put("code","200");
            result.put("msg","登录成功");
            result.put("token",subject.getSession().getId());
        } catch (Exception e) {
            result.put("code","400");
            result.put("msg","登录失败");
        }
        return result;
    }
}
